/*初始化datagrid*/
$(function () {



    /*抽取常用组件*/
    var emp_list = $("#emp_list");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");
    var check_password = $("#check_password")
    var check_password_form = $("#check_password_form")
    /*对象管理方法*/
    var object = {
        cancel: function () {
            emp_dialog.dialog('close');
        }
        ,
        commit:function(){
                check_password_form.form("clear")
                check_password.dialog("open")
        },

        save: function () {
            check_password_form.form("submit",{
                url: '/payment/checkpassword.do',
                success: function (data) {
                    var res = $.parseJSON(data);
                    if (res.success) {
                        $.messager.alert('温馨提示', res.msg, 'info', function () {
                            emp_dialog.dialog('close');
                            check_password.dialog('close');
                            emp_form.form('submit', {
                                    url: '/payment/pay.do',
                                    success: function (data) {
                                        var res = $.parseJSON(data);
                                        if (res.success) {
                                            $.messager.alert('温馨提示', res.msg, 'info', function () {
                                                emp_list.datagrid('reload');
                                            });
                                        } else {
                                            $.messager.alert('温馨提示', res.msg, 'error');
                                        }
                                    }
                                }
                            );
                            emp_list.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', res.msg, 'error');
                    }
                }
            })
        },
        pay: function () {
            $("#beginDate").datetimebox({
                editable:false,
                required:'true',
                //validType:'datetime',
                //validateOnBlur:true,
                //showSeconds:false,
                missingMessage:'请输入起保日期',
                //invalidMessage:'请输入正确的时间',
                formatter: function (date) {
                    var y = date.getFullYear();
                    var m = date.getMonth()+1;
                    var d = date.getDate();
                    return y+'-'+m+'-'+d;
                }
            });
            $("#endDate").datetimebox({
                editable:false,
                required:true,
                //validType:'datetime',
                //validateOnBlur:true,
                //showSeconds:false,
                missingMessage:'请输入止保日期,注意不要在起保日期之前',
                //invalidMessage:'请输入正确的时间',
                formatter: function (date) {
                    var y = date.getFullYear();
                    var m = date.getMonth()+1;
                    var d = date.getDate();
                    return y+'-'+m+'-'+d;
                }
            });
            var row = emp_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择要缴费的保单!', 'info');
            } else {
                emp_form.form('clear');

                row.Iid=row.id
                row['client.name'] = row.client.name;
                row['client.id_card'] = row.client.id_card;
                row['productItem.name'] = row.productItem.name;
                emp_form.form('load', row);
                $.get("/insuranceTS/getCarproductByInsuId.do", {InsuId: row.id}, function (date) {
                    row.productItem.items = date;
                    row.products = row.productItem.items;
                    var ids = $.map(row.products, function (item) {
                        return item.name;
                    })
                    $("#selectedProduct").textbox('setValue', ids);
                })


                //emp_dialog.dialog('setTitle','缴费');
                emp_dialog.dialog('open');
            }
        }
        ,
        query: function () {
            var keyword = $("#keyword").textbox('getValue');

            emp_list.datagrid('load', {
                keyword: keyword
            });
        },

    }


    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        object[cmd]();
    })

    emp_list.datagrid({
        fit: true,
        rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#emp_toolbar',
        url: '/payment/query.do',
        columns: [[
            {field: 'client', title: '投保客户', width: 100, formatter: function (value, row, index) {
                return value ? value.name : '';
            }},
            {field: 'sn', title: '核保单号', width: 100},
            {field: 'amount', title: '需缴金额', width: 100},
            {field: 'verifytime', title: '核保日期', width: 100},
            {field: 'state', title: '缴费状态', width: 100, formatter: function (value, row, index) {
                return value? '<font color="red">未缴费</font>' : '未知状态';
            }},
            {field: 'product', title: '保险机构', width: 100, formatter: function (value, row, index) {
                return row.productItem != null?row.productItem.name:'该机构不存在';
            }},
        ]]
    })

    emp_dialog.dialog({
        title:'请完善保单信息',
        closed: true,
        width: 380,
        height: 550,
        buttons: $("#emp_diaButtons"),

    })

    check_password.dialog({
        title:'密码验证',
        closed: true,
        width: 430,
        height: 200,
        buttons: $("#cheack_password"),
    })
})
